#include "device_data.h"
#include "ui_device_data.h"

device_data::device_data(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::device_data)
{
    ui->setupUi(this);
    deviceInit();
    QObject::connect(GlobalVal::mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     this, SLOT(dataUpSlot(QByteArray)));
    //上报模式连接
    connect(ui->pushButtonFlush,SIGNAL(clicked()),this,SLOT(DataDownSlot()));
    connect(ui->pushButtonChange,SIGNAL(clicked()),this,SLOT(DataDownSlot()));
    connect(ui->pushButtonStop,SIGNAL(clicked()),this,SLOT(DataDownSlot()));
    connect(ui->pushButtonPeriod,SIGNAL(clicked()),this,SLOT(DataDownSlot()));
    connect(ui->pushButtonAuto,SIGNAL(clicked()),this,SLOT(modeSlot()));
    connect(ui->pushButtonHand,SIGNAL(clicked()),this,SLOT(modeSlot()));
    //控制设备
    connect(ui->pushButtonBee,SIGNAL(toggled(bool)),this,SLOT(ButtonSlot(bool)));
    connect(ui->pushButtonFan,SIGNAL(toggled(bool)),this,SLOT(ButtonSlot(bool)));
    connect(ui->pushButtonValve,SIGNAL(toggled(bool)),this,SLOT(ButtonSlot(bool)));
    connect(ui->pushButtonDetergent,SIGNAL(toggled(bool)),this,SLOT(ButtonSlot(bool)));
    connect(ui->pushButtonHistory,SIGNAL(clicked()),this,SLOT(historySlot()));
    //发JSON格式
    connect(this,SIGNAL(ButtonSignal(struct ControlCommand)),this,SLOT(cntlDownSlot(struct ControlCommand)));
}

device_data::~device_data()
{
    delete ui;
}

void device_data::dataUpSlot(const QByteArray& loadpay)
{

    QString text;
    text.prepend(loadpay);
    qDebug()<<"text="<<text;
    getJSON(text);
}

void device_data::DataDownSlot()//数据上报模式切换
{
    if(sender()==ui->pushButtonFlush)
    {
        QByteArray payload="{ \n"
                           "    \"type\": 1, \n"
                           "    \"limit\": \"all\", \n"
                           "    \"keys\": [1, 2, 3, 4] \n"
                           "}";
        QMQTT::Message msg;
        msg.setTopic("/app/control/down");
        msg.setPayload(payload);
       // qDebug()<<"topic="<<msg.topic();
       // qDebug()<<"payload="<<msg.payload();
        GlobalVal::mqtt->publish(msg);
    }
    else if(sender()==ui->pushButtonStop)
    {
        QByteArray payload="{\n \"type\": 3, \n \"data\": {\n\"type\": 0\n}\n}";
        QMQTT::Message msg;
        msg.setTopic("/app/control/down");
        msg.setPayload(payload);
      //  qDebug()<<"topic="<<msg.topic();
      //  qDebug()<<"payload="<<msg.payload();
        GlobalVal::mqtt->publish(msg);
    }
    else if(sender()==ui->pushButtonChange)
    {
        QByteArray payload="{\n \"type\": 3, \n \"data\": {\n\"type\": 1\n}\n}";
        QMQTT::Message msg;
        msg.setTopic("/app/control/down");
        msg.setPayload(payload);
      //  qDebug()<<"topic="<<msg.topic();
       // qDebug()<<"payload="<<msg.payload();
        GlobalVal::mqtt->publish(msg);
    }
    else if(sender()==ui->pushButtonPeriod)
   {
        QByteArray payload="{\n \
                               \"type\": 3, \n\t \
                               \"data\": {\n\
                                      \t\t\"type\": 2,\n\
                                      \t\t\"period\":5\
                                          }\n\
                                }";
        QMQTT::Message msg;
        msg.setTopic("/app/control/down");
        msg.setPayload(payload);
       // qDebug()<<"topic="<<msg.topic();
      //  qDebug()<<"payload="<<msg.payload();
        GlobalVal::mqtt->publish(msg);
   }
}

void device_data::cntlDownSlot(const struct ControlCommand& cmd)
{
    QMQTT::Message msg;
    qDebug()<<"cntlDown:val"<<cmd.data.val;
    QByteArray payload=tocntlJSON(cmd).toUtf8();
    msg.setTopic("/app/control/down");
    msg.setPayload(payload);
    qDebug()<<"---------------cmd payload="<<payload;
    GlobalVal::mqtt->publish(msg);
}

void device_data::getJSON(QString &json)
{
    qDebug()<<"json===="<<json;
    QJsonParseError jsonerror;
    QJsonDocument doc = QJsonDocument::fromJson(json.toLatin1(), &jsonerror);//解析JSON，将JSON转化为UTF-8
        if (!doc.isNull() && jsonerror.error == QJsonParseError::NoError)//确保有数据且JSON格式正确
        {
            if(doc.isObject())//对象类型{}
            {
                QJsonObject object = doc.object();
                QStringList list = object.keys();//获取键值
                QJsonObject::iterator it = object.begin();//迭代器
                while(it != object.end())
                {
                    switch(it.value().type())
                    {
                    case QJsonValue::String:
                    {
                       // qDebug() << "Type is string";
                        QString sub = it.value().toString();
                        //qDebug() << it.key() << "=" << sub;
                        break;
                    }
                    case QJsonValue::Array:
                    {
                       // qDebug() << "Type is Array";
                       // qDebug() << it.key() << "=" << it.value().toArray();
                        QJsonArray sub = it.value().toArray();
                        QJsonObject dataObject;
                        QString str;
                       // qDebug()<<"subsize="<<sub.count();
                        for(int i=0;i<sub.count();i++)
                        {
                            dataObject=sub.at(i).toObject();
                            if(dataObject.value("key").toInt()==ui->lineEditN->property("key").toInt())
                             {
                                    str=dataObject.value("val").toString().append("mg/L");
//                                  str=dataObject.value("val").toString().append("℃");
                                     qDebug()<<"N val="<<str;
                                     ui->lineEditN->clear();
                                     ui->lineEditN->setText(str);
                             }
                            else  if(dataObject.value("key").toInt()==ui->lineEditP->property("key").toInt())
                            {
                                    str=dataObject.value("val").toString().append("mg/L");
//                                 str=dataObject.value("val").toString().append("%RH");
                                    qDebug()<<"P val="<<str;
                                    ui->lineEditP->clear();
                                    ui->lineEditP->setText(str);
                            }
                            else  if(dataObject.value("key").toInt()==ui->lineEditPH->property("key").toInt())
                            {
                                    str=dataObject.value("val").toString();
                                    //str=dataObject.value("val").toString().append("lx");
                                    qDebug()<<"pH val="<<str;
                                    ui->lineEditPH->clear();
                                    ui->lineEditPH->setText(str);
                            }
                            else  if(dataObject.value("key").toInt()==ui->lineEditWaterSpeed->property("key").toInt())
                            {
                                    str=dataObject.value("val").toString().append("m/s");
                                    //str=dataObject.value("val").toString().append("ppm");
                                    qDebug()<<"waterspeed val="<<str;
                                    ui->lineEditWaterSpeed->clear();
                                    ui->lineEditWaterSpeed->setText(str);
                            }
                            else  if(dataObject.value("key").toInt()==ui->lineEditCO2->property("key").toInt())
                            {
                                    str=dataObject.value("val").toString().append("ppm");
                                    qDebug()<<"CO2 val="<<str;
                                    ui->lineEditCO2->clear();
                                    ui->lineEditCO2->setText(str);
                            }
                            else  if(dataObject.value("key").toInt()==ui->lineEditDO->property("key").toInt())
                            {
                                    str=dataObject.value("val").toString().append("mg/L");
                                    qDebug()<<"DO val="<<str;
                                    ui->lineEditDO->clear();
                                    ui->lineEditDO->setText(str);
                            }
                            else  if(dataObject.value("key").toInt()==ui->pushButtonBee->property("key").toInt())
                            {
                                    qDebug()<<" 蜂鸣器状态："<<dataObject.value("val").toInt();
                                    if(dataObject.value("val").toInt()==1)
                                    {
                                        ui->pushButtonBee->setText("警报开");
                                    }
                                    else if(dataObject.value("val").toInt()==0)
                                    {
                                        ui->pushButtonBee->setText("警报关");
                                    }
                            }
                            else  if(dataObject.value("key").toInt()==ui->pushButtonFan->property("key").toInt())
                            {
                                    qDebug()<<" 风扇状态："<<dataObject.value("val").toInt();
                                    if(dataObject.value("val").toInt()==1)
                                    {
                                        ui->pushButtonFan->setText("搅拌开");
                                    }
                                    else if(dataObject.value("val").toInt()==0)
                                    {
                                        ui->pushButtonFan->setText("搅拌关");
                                    }
                            }
                            else  if(dataObject.value("key").toInt()==ui->pushButtonValve->property("key").toInt())
                            {
                                    qDebug()<<" 阀门状态："<<dataObject.value("val").toInt();
                                    if(dataObject.value("val").toString()=="valve=1")
                                    {
                                        ui->pushButtonValve->setText("阀门开");
                                    }
                                    else if(dataObject.value("val").toString()=="valve=0")
                                    {
                                        ui->pushButtonValve->setText("阀门关");
                                    }
                            }
                            else  if(dataObject.value("key").toInt()==ui->pushButtonDetergent->property("key").toInt())
                            {
                                    qDebug()<<" 加药状态："<<dataObject.value("val").toInt();
                                    if(dataObject.value("val").toString()=="clean=1")
                                    {
                                        ui->pushButtonDetergent->setText("加药开");
                                    }
                                    else if(dataObject.value("val").toString()=="clean=0")
                                    {
                                        ui->pushButtonDetergent->setText("加药关");
                                    }
                            }
                        }
                        break;
                    }
                    case QJsonValue::Bool:
                    {
                      //  qDebug() << "Type is Bool";
                      //  qDebug() << it.key() << "=" << it.value().toBool();
                        break;
                    }
                    case QJsonValue::Double:
                    {
                       // qDebug() << "Type is Double";
                       // qDebug() << it.key() << "=" << it.value().toDouble();
                        break;
                    }
                    case QJsonValue::Object:
                    {
                       // qDebug() << "Type is Object";
                        //qDebug() << it.key() << "=" << it.value().toObject();
                        QJsonObject childObj = it.value().toObject();
                        QJsonDocument childDoc(childObj);
                        // 先存储到 QString 变量
                        QString jsonStr = QString::fromUtf8(childDoc.toJson(QJsonDocument::Indented));
                        getJSON(jsonStr); // 传递变量
                        break;
                    }
                    case QJsonValue::Null:
                    {
                       // qDebug() << "Type is Null";
                       // qDebug() << it.key() << "= NULL" ;
                        break;
                    }
                    case QJsonValue::Undefined:
                    {
                        //qDebug() << "Type is Undefined";
                        break;
                    }
                    }
                    it++;
                }
            }
        }
}

void device_data::deviceInit()
{
    QFile file("C:/Users/90613/Desktop/z_zu/qt_-repository/gateway_app/node.json");//读取点表

   // QFile file("D:/Project/develop/qt_-repository/gateway_app/node.json");//读取点表
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Failed to open file!";
    }
    QByteArray jsonData = file.readAll();
    file.close();
    QString node = QString::fromUtf8(jsonData);
    nodejson(node);


}
//解析点表
void device_data::nodejson(QString &node)
{
    QJsonParseError jsonerror;
    QJsonDocument doc = QJsonDocument::fromJson(node.toLatin1(), &jsonerror);//解析JSON，将JSON转化为UTF-8
        if (!doc.isNull() && jsonerror.error == QJsonParseError::NoError)//确保有数据且JSON格式正确
        {
            if(doc.isObject())//对象类型{}
            {
                QJsonObject object = doc.object();
                QJsonObject::iterator it = object.begin();//迭代器
                while(it != object.end())
                {
                    switch(it.value().type())
                    {
                    case QJsonValue::String:
                    {
//                        qDebug() << "Type is string";
                        QString sub = it.value().toString();
//                        qDebug() << it.key() << "=" << sub;
                        if(it.key()=="version")
                        {
                            version=it.value().toString();
//                            qDebug()<<"version=="<<version;
                        }
                        break;
                    }
                    case QJsonValue::Array:
                    {
//                        qDebug() << "Type is Array";
//                        qDebug() << it.key() << "=" << it.value().toArray();
                        QJsonArray sub = it.value().toArray();
                        QJsonObject dataObject;
                        QString str;
                        qDebug()<<"subsize="<<sub.count();
                        for(int i=0;i<sub.count();i++)
                        {
                            dataObject=sub.at(i).toObject();
                            if(dataObject.value("name").toString()=="Nitrogen")
                            {
                                ui->lineEditN->setProperty("key",dataObject.value("key").toInt());
                                ui->lineEditN->setProperty("name",dataObject.value("name").toString());
                                ui->lineEditN->setProperty("type",dataObject.value("type").toInt());
                                qDebug()<<"Nitrogen key=="<<ui->lineEditN->property("key");
                            }
                            else if(dataObject.value("name").toString()=="Phosphorus")
                            {
                                ui->lineEditP->setProperty("key",dataObject.value("key").toInt());
                                ui->lineEditP->setProperty("name",dataObject.value("name").toString());
                                ui->lineEditP->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="Warner")
                            {
                                ui->pushButtonBee->setProperty("key",dataObject.value("key").toInt());
                                ui->pushButtonBee->setProperty("name",dataObject.value("name").toString());
                                ui->pushButtonBee->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="Mixer")
                            {
                                ui->pushButtonFan->setProperty("key",dataObject.value("key").toInt());
                                ui->pushButtonFan->setProperty("name",dataObject.value("name").toString());
                                ui->pushButtonFan->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="PH_num")
                            {
                                ui->lineEditPH->setProperty("key",dataObject.value("key").toInt());
                                ui->lineEditPH->setProperty("name",dataObject.value("name").toString());
                                ui->lineEditPH->setProperty("addr",dataObject.value("addr").toInt());
                                ui->lineEditPH->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="water_flow")
                            {
                                ui->lineEditWaterSpeed->setProperty("key",dataObject.value("key").toInt());
                                ui->lineEditWaterSpeed->setProperty("name",dataObject.value("name").toString());
                                ui->lineEditWaterSpeed->setProperty("addr",dataObject.value("addr").toInt());
                                ui->lineEditWaterSpeed->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="CO2_num")
                            {
                                ui->lineEditCO2->setProperty("key",dataObject.value("key").toInt());
                                ui->lineEditCO2->setProperty("name",dataObject.value("name").toString());
                                ui->lineEditCO2->setProperty("addr",dataObject.value("addr").toInt());
                                ui->lineEditCO2->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="DO_num")
                            {
                                ui->lineEditDO->setProperty("key",dataObject.value("key").toInt());
                                ui->lineEditDO->setProperty("name",dataObject.value("name").toString());
                                ui->lineEditDO->setProperty("addr",dataObject.value("addr").toInt());
                                ui->lineEditDO->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="valve")
                            {
                                ui->pushButtonValve->setProperty("key",dataObject.value("key").toInt());
                                ui->pushButtonValve->setProperty("name",dataObject.value("name").toString());
                                ui->pushButtonValve->setProperty("addr",dataObject.value("addr").toInt());
                                ui->pushButtonValve->setProperty("type",dataObject.value("type").toInt());
                            }
                            else if(dataObject.value("name").toString()=="detergent")
                            {
                                ui->pushButtonDetergent->setProperty("key",dataObject.value("key").toInt());
                                ui->pushButtonDetergent->setProperty("name",dataObject.value("name").toString());
                                ui->pushButtonDetergent->setProperty("addr",dataObject.value("addr").toInt());
                                ui->pushButtonDetergent->setProperty("type",dataObject.value("type").toInt());
                            }

                        }
                        break;
                    }
                    case QJsonValue::Bool:
                    {
                        qDebug() << "Type is Bool";
                        qDebug() << it.key() << "=" << it.value().toBool();
                        break;
                    }
                    case QJsonValue::Double:
                    {
                        qDebug() << it.key() << "=" << it.value().toDouble();
                        double value = it.value().toDouble();
                           qint64 intValue = static_cast<qint64>(value);
                           // 判断是否为整数（通过比较双精度值和其整数截断后的值）
                           if (value == intValue) {
//                               qDebug() << "Type is Integer";
//                               if(it.key()=="type")
//                               {
//                                   rep.type=it.value().toInt();
//                               }
//                               else if(it.key()=="period")
//                               {
//                                   rep.period=it.value().toInt();
//                               }

                           }
                           else {
                               qDebug() << "Type is Double";
                               qDebug() << it.key() << "=" << value;
                           }
                        break;
                    }
                    case QJsonValue::Object:
                    {
                        qDebug() << "Type is Object";
                        qDebug() << it.key() << "=" << it.value().toObject();
                        QJsonObject obj=it.value().toObject();
                        if(it.key()=="report")
                        {
                            rep.type=obj.value("type").toInt();
                            rep.period=obj.value("period").toInt();
                            qDebug()<<"report:"<<rep.type<<"---"<<rep.period;
                        }
                        else if(it.key()=="mqtt_server")
                        {
                            mqtt_server.addr=obj.value("addr").toString();
                            mqtt_server.port=obj.value("port").toInt();
                            qDebug()<<"mqttserver:"<<mqtt_server.addr<<"---"<<mqtt_server.port;
                        }
                        else if(it.key()=="mb_dev")
                        {
                            mb_dev.addr=obj.value("addr").toString();
                            mb_dev.port=obj.value("port").toInt();
                            qDebug()<<"modbus_dev:"<<mb_dev.addr<<"---"<<mb_dev.port;
                        }
                        else
                        {
                            QJsonObject childObj = it.value().toObject();
                            QJsonDocument childDoc(childObj);
                            // 先存储到 QString 变量
                            QString jsonStr = QString::fromUtf8(childDoc.toJson(QJsonDocument::Indented));
                            nodejson(jsonStr); // 传递变量
                        }
                        break;
                    }
                    case QJsonValue::Null:
                    {
                        qDebug() << "Type is NuLL";
                        qDebug() << it.key() << "= NULL" ;
                        break;
                    }
                    case QJsonValue::Undefined:
                    {
                        qDebug() << "Type is Undefined";
                        break;
                    }
                    }
                    it++;
                }
            }
        }
}
//控制设备
void device_data::ButtonSlot(bool checked)
{
    if(sender()==ui->pushButtonFan)
    {
        if(checked)
        {
            ui->pushButtonFan->setText("搅拌开");
            ui->pushButtonFan->setProperty("val","FAN_ON");
        }
        else
        {
            ui->pushButtonFan->setText("搅拌关");
            ui->pushButtonFan->setProperty("val","FAN_OFF");
        }
        struct ControlCommand cmd;
        //cmd.type=ui->pushButtonFan->property("type").toInt();
        cmd.type=2;
        cmd.data.msg=1;
        cmd.data.key=ui->pushButtonFan->property("key").toInt();
        cmd.data.val=ui->pushButtonFan->property("val").toString();
        emit ButtonSignal(cmd);
    }
    else  if(sender()==ui->pushButtonBee)
    {
        if(checked)
        {
            ui->pushButtonBee->setText("警报开");
            ui->pushButtonBee->setProperty("val","BUZZ_ON");
        }
        else
        {
            ui->pushButtonBee->setText("警报关");
            ui->pushButtonBee->setProperty("val","BUZZ_OFF");
        }
        struct ControlCommand cmd;
       // cmd.type=ui->pushButtonBee->property("type").toInt();
        cmd.type=2;
        cmd.data.msg=1;
        cmd.data.key=ui->pushButtonBee->property("key").toInt();
        cmd.data.val=ui->pushButtonBee->property("val").toString();
        emit ButtonSignal(cmd);
    }
    else  if(sender()==ui->pushButtonValve)
    {
        if(checked)
        {
            ui->pushButtonValve->setText("阀门开");
            ui->pushButtonValve->setProperty("val","valve=1");
        }
        else
        {
            ui->pushButtonValve->setText("阀门关");
            ui->pushButtonValve->setProperty("val","valve=0");
        }
        struct ControlCommand cmd;
       // cmd.type=ui->pushButtonValve->property("type").toInt();
        cmd.type=2;
        cmd.data.msg=2;
        cmd.data.key=ui->pushButtonValve->property("key").toInt();
        cmd.data.val=ui->pushButtonValve->property("val").toString();
        emit ButtonSignal(cmd);
    }
    else  if(sender()==ui->pushButtonDetergent)
    {
        if(checked)
        {
            ui->pushButtonDetergent->setText("加药开");
            ui->pushButtonDetergent->setProperty("val","clean=1");
        }
        else
        {
            ui->pushButtonDetergent->setText("加药关");
            ui->pushButtonDetergent->setProperty("val","clean=0");
        }
        struct ControlCommand cmd;
        //cmd.type=ui->pushButtonDetergent->property("type").toInt();
         cmd.type=2;
        cmd.data.msg=2;
        cmd.data.key=ui->pushButtonDetergent->property("key").toInt();
        cmd.data.val=ui->pushButtonDetergent->property("val").toString();
        emit ButtonSignal(cmd);
    }

}

void device_data::historySlot()
{
    History *his=new History();
    his->show();
}

void device_data::modeSlot()
{
    if(sender()==ui->pushButtonAuto)
    {
        ui->pushButtonAuto->setEnabled(false);
        ui->pushButtonHand->setEnabled(true);
        ui->pushButtonBee->setEnabled(false);
        ui->pushButtonChange->setEnabled(false);
        ui->pushButtonDetergent->setEnabled(false);
        ui->pushButtonFan->setEnabled(false);
        ui->pushButtonFlush->setEnabled(false);
        ui->pushButtonPeriod->setEnabled(false);
        ui->pushButtonStop->setEnabled(false);
        ui->pushButtonValve->setEnabled(false);
        QJsonObject obj;
        obj["type"]=5;
        obj["mode"]=1;
        obj["N_key"]=ui->lineEditN->property("key").toInt();
        obj["N_val"]=ui->doubleSpinBoxN->value();
        obj["P_key"]=ui->lineEditP->property("key").toInt();
        obj["P_val"]=ui->doubleSpinBoxP->value();
//        QJsonArray dataArray;
//        QJsonObject item1;
//        item1["key"]=ui->lineEditN->property("key").toInt();
//        item1["val"]=ui->doubleSpinBoxN->value();
//        dataArray.append(item1);
//        QJsonObject item2;
//        item2["key"]=ui->lineEditP->property("key").toInt();
//        item2["val"]=ui->doubleSpinBoxP->value();
//        dataArray.append(item2);
//        obj["data"]=dataArray;
        QJsonDocument doc(obj);
        QByteArray byteArray=doc.toJson(QJsonDocument::Compact);
        qDebug()<<"自动"<<byteArray;
        QMQTT::Message msg;
        msg.setTopic("/app/control/down");
        msg.setPayload(byteArray);
         GlobalVal::mqtt->publish(msg);
    }
    else  if(sender()==ui->pushButtonHand)
    {
        ui->pushButtonAuto->setEnabled(true);
        ui->pushButtonHand->setEnabled(false);
        ui->pushButtonBee->setEnabled(true);
        ui->pushButtonChange->setEnabled(true);
        ui->pushButtonDetergent->setEnabled(true);
        ui->pushButtonFan->setEnabled(true);
        ui->pushButtonFlush->setEnabled(true);
        ui->pushButtonPeriod->setEnabled(true);
        ui->pushButtonStop->setEnabled(true);
        ui->pushButtonValve->setEnabled(true);
        QJsonObject obj;
        obj["type"]=5;
        obj["mode"]=0;
//        QJsonArray dataArray;
//        QJsonObject item1;
//        item1["key"]=ui->lineEditN->property("key");
//        item1["val"]=ui->doubleSpinBoxN->value();
//        dataArray.append(item1);
//        QJsonObject item2;
//        item2["key"]=ui->lineEditP->property("key");
//        item2["val"]=ui->doubleSpinBoxP->value();
//        dataArray.append(item2);
//        obj["data"]=dataArray;
        QJsonDocument doc(obj);
        QByteArray byteArray=doc.toJson(QJsonDocument::Compact);
//        qDebug()<<"自动"<<byteArray;
        QMQTT::Message msg;
        msg.setTopic("/app/control/down");
        msg.setPayload(byteArray);
         GlobalVal::mqtt->publish(msg);
    }
}

QString device_data::tocntlJSON(const ControlCommand &cmd)//返回JSON字符串
{
    QJsonObject root;
    root["type"]=cmd.type;
    QJsonObject dataObj;
    dataObj["msg"]=cmd.data.msg;
    dataObj["key"]=cmd.data.key;
    dataObj["val"]=cmd.data.val;
    root["data"]=dataObj;
//    qDebug()<<"JSON:"<<cmd.type<<"---"<<cmd.data.key<<"---"<<cmd.data.msg<<"---"<<cmd.data.val;
    return QJsonDocument(root).toJson(QJsonDocument::Indented);//带格式转换

}
